#include "options.h"
#include "ui_options.h"
#include <QDebug>


options::options(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::options)
{
    QSettings Ini(QCoreApplication::applicationDirPath()+"/config.ini", QSettings::IniFormat, this);
    Ini.setIniCodec("UTF-8");

    ui->setupUi(this);

    //QStringList m_serialPortName;
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
        ui->portNameComboBox->addItem(info.portName());
    }
    ui->portNameComboBox->setCurrentText(Ini.value("com/portname").toString());
    ui->spinBox->setValue(Ini.value("com/delytime",50).toInt());
    ui->spinBox_Day->setValue(Ini.value("sys/day",7).toInt());
    ui->spinBox_overtime->setValue(Ini.value("sys/overtimecount", 1).toInt());
    //qDebug() << Ini.value("sys/mp3", 0).toInt();
    //qDebug() << Ini.value("sys/mp3_random", false).toBool();

    standItemModel = new QStandardItemModel();
    ui->tableView->setModel(standItemModel);
    //添加表头
    standItemModel->setColumnCount(4);
    standItemModel->setRowCount(10);
    standItemModel->setHeaderData(0,Qt::Horizontal,QStringLiteral("卡槽号"));   //设置表头内容
    standItemModel->setHeaderData(1,Qt::Horizontal,QStringLiteral("Modbus 地址"));
    standItemModel->setHeaderData(2,Qt::Horizontal,QStringLiteral("A通道名称"));
    standItemModel->setHeaderData(3,Qt::Horizontal,QStringLiteral("B通道名称"));

    for(int i=1; i<=MSA9020_COUNT; i++) {
        standItemModel->setItem(i-1, 0, new QStandardItem(QString::number(i)+"#卡槽"));
        standItemModel->setItem(i-1, 1, new QStandardItem(Ini.value("msa/addr"+QString::number(i),0).toString()));
        standItemModel->setItem(i-1, 2, new QStandardItem(Ini.value("msa/a"+QString::number(i),"区域A"+QString::number(i)).toString()));
        standItemModel->setItem(i-1, 3, new QStandardItem(Ini.value("msa/b"+QString::number(i),"区域B"+QString::number(i)).toString()));
        standItemModel->item(i-1, 0)->setFlags((Qt::NoItemFlags)); //只读
    }
}

options::~options()
{
    delete ui;
}



void options::on_btn_save_clicked()
{
    QSettings Ini(QCoreApplication::applicationDirPath()+"/config.ini", QSettings::IniFormat, this);

    Ini.setValue("com/portname", ui->portNameComboBox->currentText());
    //qDebug() << ui->portNameComboBox->currentText();
    Ini.setValue("com/delytime", ui->spinBox->value());
    Ini.setValue("sys/day", ui->spinBox_Day->value());
    Ini.setValue("sys/overtimecount",ui->spinBox_overtime->value());
    Ini.setValue("sys/mp3",ui->comboBox->currentIndex());
    Ini.setValue("sys/mp3_random",ui->rb_random->isChecked());

    for(int i=1; i<=MSA9020_COUNT; i++) {
        Ini.setValue("msa/addr"+QString::number(i), standItemModel->item(i-1, 1)->text());
        Ini.setValue("msa/a"+QString::number(i), standItemModel->item(i-1, 2)->text());
        Ini.setValue("msa/b"+QString::number(i), standItemModel->item(i-1, 3)->text());
        //qDebug() << Ini.status();
    }
    emit sendData();
    this->close();
}

void options::on_btn_cancel_clicked()
{
    this->close();
}

void options::on_pushButton_clicked()
{
    QProcess process;
    process.startDetached("python3 /usr/bin/onboard");
}

void options::add_mp3(QList<QFileInfo> *fileInfo)
{
    QSettings Ini(QCoreApplication::applicationDirPath()+"/config.ini", QSettings::IniFormat, this);
    ui->comboBox->clear();
    for(int i = 0; i<fileInfo->count(); i++)
        ui->comboBox->addItem(fileInfo->at(i).fileName());

    ui->comboBox->setCurrentIndex(Ini.value("sys/mp3", 0).toInt());
    bool b = Ini.value("sys/mp3_random", false).toBool();
    ui->rb_random->setChecked(b);
    ui->rb_assign->setChecked(!b);
#ifdef _DEBUG_MODE
    qDebug() << "mp3 index:"<< Ini.value("sys/mp3", 0).toInt();
    qDebug() << "random:" << b;
#endif
}


